package com.gljx.web.service.impl;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.ActitiviUtil;
import com.gljx.util.Axamine;
import com.gljx.util.ServerResponse;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.Recordin;
import com.gljx.web.Entity.Recordout;
import com.gljx.web.Entity.Recordoutentry;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.RecordoutMapper;
import com.gljx.web.dao.RecordoutentryMapper;
import com.gljx.web.service.RecordoutService;
import com.gljx.web.service.RecordoutentryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/7 0007.
 */
@Service
public class RecordoutServiceImpl extends CommonDao implements RecordoutService {

    @Autowired
    RecordoutentryService recordoutentryService;
    @Autowired
    RecordoutMapper recordoutMapper;
    @Autowired
    RecordoutentryMapper recordoutentryMapper;
    @Autowired
    HttpSession session;

    @Override
    public Map<String, Object> selectPageCount(String psql) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.RecordoutMapper.pagecount", paramMap, null);
        return paramMap;
    }

    @Override
    public Map<String, Object> selectPage(int pindex, String psql, int psize) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.RecordoutMapper.pagemap", paramMap, null);
        return paramMap;
    }

    /**
     * @Author lcx
     * @Date 2017/11/8 0008 下午 2:38
     * 根据fid查询档案
     */
    @Override
    public Recordout selectRecordoutByFid(String fid) {


        Recordout recordout = recordoutMapper.selectByPrimaryKey(fid);
        List<Recordoutentry> recordoutentryList = recordoutentryService.selectRecordoutentryByFid(fid);
        recordout.setRecordoutentryList(recordoutentryList);

        return recordout;
    }

    /**
     * @Author lcx
     * @Date 2017/11/9 0009 下午 4:08
     * 更新数据
     */
    @Override
    public void updateRecordout(Recordout recordout) {

        recordoutMapper.insertSelective(recordout);
    }

    @Override
    public void addRecordout(Recordout recordout) {

        recordoutMapper.insertSelective(recordout);
    }

    /**
     * @Author lcx
     * @Date 2017/11/13 0013 下午 12:28
     * 删除父类以及子类
     */
    @Override
    public void deleteByRecordoutKey(String[] deleteId,String[] fnumber) {
        ActitiviUtil actitiviUtil = new ActitiviUtil();
        //删除记录
        for (int i = 0; i < deleteId.length; i++) {
            /*过滤为空的删除操作*/
            if (!deleteId[i].equals("")) {
                actitiviUtil.deleteTask(ActivitiKey.PUBLIC_KEY ,fnumber[i]);
                recordoutMapper.deleteByPrimaryKey(deleteId[i]);
                recordoutentryMapper.deleteByFparentid(deleteId[i]);
            }
        }
    }

    @Override
    public void deleteRecordouAndRecordoutentry(String fid) {
        recordoutMapper.deleteByPrimaryKey(fid);
        recordoutentryMapper.deleteByFparentid(fid);
    }

    @Override
    public ServerResponse examine(Recordout recordout, String state, String agreed, String auditcomment) {
        //如果是审核
        Axamine axamine = new Axamine();
        if ("5".equalsIgnoreCase(state)) {
            ServerResponse serverResponse = axamine.auditProcess(recordout.getFnumber(), agreed, auditcomment);
            //如果返回 1 代表 流程结束 2代表流程还在继续 0 代表错误
            if (serverResponse.getStatus() == 1) {


                //获取操作人信息
                User user = (User) session.getAttribute("user");

                //获取当前修改时间
                String createTime = TimeFormat.timeFormat(new Date());
                //修改时间
                recordout.setFlastupdatetime(createTime);
                //修改人id
                recordout.setFauditorid(user.getFid());

                recordout.setCfbillstate(Long.valueOf("5"));
                recordoutMapper.updateByPrimaryKeySelective(recordout);
            }
            //如果是不同意状态则修改单据状态为保存
            if ("2".equals(agreed)) {
                recordout.setCfbillstate(Long.valueOf("3"));
                recordoutMapper.updateByPrimaryKeySelective(recordout);
            }
            return serverResponse;
        }
        if ("3".equalsIgnoreCase(state)) {
            recordout.setCfbillstate(Long.valueOf("3"));
            recordout.setFsourcefunction(auditcomment);
            axamine.againstAuditProcess(recordout.getFnumber(), auditcomment);
            recordoutMapper.updateByPrimaryKeySelective(recordout);
            return ServerResponse.createBySuccessMessage("反审核成功！");
        }

        return ServerResponse.createByError();
    }

    @Override
    public List<Recordoutentry> selectRecordoutXls(String fnumber, String name, String beginTime, String endTime, String fcompanyid) {
        return recordoutMapper.selectRecordoutXls(fnumber, name, beginTime, endTime, fcompanyid);

    }
}
